import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';
import '../textFiled/kq_textfiled.dart';
import 'entity/kq_form_entity.dart';

//自动生成单号
class KqNoAutoView extends StatefulWidget {
  const KqNoAutoView({super.key, required this.entity, this.onTap});
  final KqFormEntity entity;
  //点击自动生成的时候
  final Function? onTap;
  @override
  State<KqNoAutoView> createState() => _KqNoAutoViewState();
}

class _KqNoAutoViewState extends State<KqNoAutoView> {
  //左侧的必填
  Widget renderStarView() {
    var star = widget.entity.mustInput && widget.entity.editable ? '*' : '';
    return SizedBox(
      width: 16.r,
      child: Text(
        star,
        textAlign: TextAlign.center,
        style: TextStyle(
            color: Colors.red,
            fontSize: KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .mustInputFontSize ??
                24.sp),
      ),
    );
  }

  //title
  Widget renderTitleView() {
    return LimitedBox(
      maxWidth: 104.r,
      child: Text(
        widget.entity.title,
        maxLines: 1,
        style: TextStyle(
            color: KqThemeColors.text333,
            fontSize:
                KqThemeManager.instance.getConfig().formConfig.titleFontSize ??
                    16.sp),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              renderStarView(),
              renderTitleView(),
              Expanded(
                  child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                      child: Container(
                    color: Colors.white,
                    padding:
                        EdgeInsets.symmetric(vertical: 10.r, horizontal: 8.r),
                    child: KqTextFiled(
                      multiline: true,
                      maxLines: null,
                      inputType: widget.entity.inputType,
                      editable: widget.entity.editable,
                      enableClearButton: true,
                      fontSize: KqThemeManager.instance
                              .getConfig()
                              .formConfig
                              .contentFontSize ??
                          16.sp,
                      textAlign: TextAlign.end,
                      defValue: widget.entity.valueForShow ?? '',
                      placeHolder: widget.entity.hint,
                      maxCharCount: widget.entity.maxCharCount,
                      inputFormatters: widget.entity.inputFormatters,
                      onFocusChanged: (hasFocus) {},
                      onChanged: (text) {
                        widget.entity.valueForShow = text;
                        widget.entity.valueForSubmit = text;
                      },
                    ),
                  )),
                  Visibility(
                      visible: widget.entity.editable,
                      child: InkWell(
                        onTap: () {
                          if (widget.onTap != null) {
                            widget.onTap!();
                          }
                        },
                        child: Container(
                            constraints:
                                BoxConstraints(maxWidth: 90.r, minWidth: 60.r),
                            decoration: BoxDecoration(
                              color: KqThemeColors.textBlue,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(4.r)),
                            ),
                            margin: EdgeInsets.only(
                              right: 10.r,
                            ),
                            padding: EdgeInsets.symmetric(
                                horizontal: 8.r, vertical: 5.r),
                            child: Text(
                              '自动生成',
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  color: KqThemeColors.textWhite,
                                  fontSize: 12.sp),
                            )),
                      ))
                ],
              )),
            ],
          ),
          const KqDivider()
        ],
      ),
    );
  }
}
